$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'TeacherCourseContent/Select',
        datatype: "json",
        colModel: [
             { label: 'ID', name: 'classCourseId', width: 40 ,key:true,hidden:true,sortable: false },
             { label: '教师职工号', name: 'courseTeacherId', width: 25,hidden:true,sortable: false },
             { label: '教师姓名', name: 'courseTeacherName', width: 20,hidden:true,sortable: false },
             { label: '课程id', name: 'courseSchoolId', width: 80,hidden:true,sortable: false },
             { label: '课程名称', name: 'courseName', width: 30,sortable: false },
             { label: '班级名称', name: 'className', width: 60,hidden:true,sortable: false },
             { label: '教学周', name: 'courseWhichWeek',sortable: false, width: 20,formatter: function(value, options, row)
                 {
                        return "第"+value+"周";
                 }},
             { label: '星期几', name: 'courseWhichDay', width: 20,hidden:true,sortable: false },
             { label: '第几节', name: 'courseWhichIndex', width: 20,hidden:true,sortable: false },
             { label: '教学内容', name: 'teachContent', width: 40,sortable: false },
            { label: '操作', name: 'classCourseId', width: 50,sortable: false, formatter: function(value, options, row){
                    var r=
                        '&nbsp;&nbsp;<a class="detail-btn" onclick="getDetails(\'' +value+'\')">设置</a>'

                    return r;
                    // '             <a class="btn btn-primary"><i class="fa fa-trash-o"></i>&nbsp;删除</a>';
                }}
          ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $.ajax({
                     url: baseURL + "/TeacherCourseContent/selectCourseName",
                     data: {},
                     success: function (r) {
                         $("#CourseName").append("<option value=''>请选择</option>");
                         for (i = 0; i < r.list.length; i++) {
                             var tname = r.list[i];
                             var tid = r.list[i];
                             $("#CourseName").append("<option value='" + tid + "'>" + tname + "</option>");
                         }
                     }
        });
});

function getDetails(id) {
    if(id == null){
        return ;
    }
    $.get(baseURL + "TeacherCourseContent/Info/"+id, function(r){
        vm.showList = false;
        vm.title = "修改";
        vm.TeacherCourseContent = r.TeacherCourseContent;
        vm.classCourseId = r.TeacherCourseContent.classCourseId;
    });
}

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        TeacherCourseContent: {},
        classCourseId: null,
        teacherCount:null,
        boolean:null
    },
    mounted(){
       //  this.GetContentCount()
    },
    methods: {
        query: function () {
            vm.reload();
        },
        CourseNameChange:function () {
            vm.reload();
            vm.GetContentCount();
        }
        ,
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.TeacherCourseContent = {};
        },
        GetContentCount: function () {
                $.get(baseURL + "TeacherCourseContent/Count/"+$("#CourseName").val(),

                    function(r){
                    vm.teacherCount = r.teacherCount;
                });
         },

        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "TeacherCourseContent/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.TeacherCourseContent = r.TeacherCourseContent;
                vm.classCourseId = r.TeacherCourseContent.classCourseId;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "TeacherCourseContent/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }
                $.ajax({
                            type: "POST",
                            url: baseURL + "TeacherCourseContent/UpdateAll",
                            contentType: "application/json",
                            data: JSON.stringify(vm.TeacherCourseContent),
                            success: function(r){
                                if(r.code === 0){
                                    alert('操作成功', function(){
                                        vm.reload();
                                    });
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
            vm.GetContentCount();
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'teacherId': vm.q.key,
                 'courseName': $("#CourseName").val()
                },
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.TeacherCourseContent.teachContent)){
                alert("教学内容不能为空");
                return true;
            }
            }
        }
});